using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;

namespace AliCMS.SystemModule.Bll
{
    public class Client : DbObjectBase, IClient
    {
		public Client(int instanceID, string lang, bool checkRight, string database)
			: base(instanceID, lang, checkRight, database)
		{
		}

		public override object ExecuteService(HttpContext context)
		{
            object result = null;

            string operation = context.Request.QueryString["Operation"];
            if (operation == "Add")
            {
                Dictionary<string, object> item = new Dictionary<string, object>();
                string value = null;
                value = context.Request.Form["Name"];
                if (value != null) item[Alias.Client.Name + Lang] = value;
                value = context.Request.Form["RoleID"];
                if (value != null) item[Alias.Client.RoleID] = value;
				value = context.Request.Form["Settings"];
				if (value != null) item[Alias.Client.Settings] = value;

                result = Add(item);
            }
            else if (operation == "Modify")
            {
                int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                Dictionary<string, object> changes = new Dictionary<string, object>();
                string value = null;
                value = context.Request.Form["Name"];
                if (value != null) changes[Alias.Client.Name + Lang] = value;
                value = context.Request.Form["RoleID"];
                if (value != null) changes[Alias.Client.RoleID] = value;
				value = context.Request.Form["Settings"];
				if (value != null) changes[Alias.Client.Settings] = value;

                Modify(id, changes);
            }
            else if (operation == "Delete")
            {
                int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                Delete(id);
            }
            else if (operation == "List")
            {
                string value = null;

                int page = -1;
                value = context.Request.QueryString["Page"];
                if (value != null) page = Convert.ToInt32(value);

                int pageSize = -1;
                value = context.Request.QueryString["PageSize"];
                if (value != null) pageSize = Convert.ToInt32(value);

                string condition = context.Request.QueryString["Condition"];
                string sort = context.Request.QueryString["Sort"];

                result = List(page, pageSize, condition, sort);
            }
            else
            {
                throw new Exception("lang_header_param");
            }

            return result;
        }

		public int GetInnerIDFromGuid(Guid guid)
        {
            return Dal.GetInnerIDFromGuid(guid);
        }
		public Guid GetGuidFromInnerID(object id)
        {
            return Dal.GetGuidFromInnerID(id);
        }
		public Dictionary<string, object> Get(object id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Get", guid);
            }

            return Dal.Get(id);
        }
		public Guid Add(Dictionary<string, object> item)
        {
            if (CheckRight)
            {
                ValidateRight("Add", Guid.Empty);
            }

            if (!item.ContainsKey(Alias.Client.ID) || item[Alias.Client.ID].ToString() == String.Empty)
                item[Alias.Client.ID] = Guid.NewGuid();
            else if (!(item[Alias.Client.ID] is Guid))
                item[Alias.Client.ID] = new Guid(item[Alias.Client.ID].ToString());

			if (item.ContainsKey(Alias.Client.RoleID))
			{
				string roleID = Convert.ToString(item[Alias.Client.RoleID]);
				if (String.IsNullOrEmpty(roleID))
					item[Alias.Client.RoleID] = DBNull.Value;
				else
					item[Alias.Client.RoleID] = new Guid(roleID);
			}

			item[Alias.Client.Secret] = Guid.NewGuid().ToString().Replace("-", "");

            Dal.Add(item);

			Dal.EndWork();
			Manager.UpdateClients();

            return (Guid)item[Alias.Client.ID];
        }
		public void Modify(int id, Dictionary<string, object> changes)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Modify", guid);
            }

			if (changes.ContainsKey(Alias.Client.RoleID))
			{
				string roleID = Convert.ToString(changes[Alias.Client.RoleID]);
				if (String.IsNullOrEmpty(roleID))
					changes[Alias.Client.RoleID] = DBNull.Value;
				else
					changes[Alias.Client.RoleID] = new Guid(roleID);
			}

            Dal.Modify(id, changes);

			Dal.EndWork();
			Manager.UpdateClients();
        }
		public void Delete(int id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Delete", guid);
            }

            Dal.Delete(id);

			Dal.EndWork();
			Manager.UpdateClients();
        }
		public DataTable List(int page, int pageSize, string condition, string sort)
        {
            if (CheckRight)
            {
                ValidateRight("List", Guid.Empty);
            }

            return Dal.List(page, pageSize, condition, sort);
        }
		public int GetRecordCount(string condition)
        {
            return Dal.GetRecordCount(condition);
        }

        #region Properties
        private Dal.IClient Dal
        {
            get { return (Dal.IClient)DalBase; }
        }
        #endregion
    }
}
